/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Painel.java
 *
 * Created on 29/06/2010, 10:45:04
 */
package GUI;

import fuzzy.ControleFuzzy;
import java.awt.Dimension;
import javax.swing.JToolBar;
import sistema_fuzzy.Robo;

/**
 * @author Thay
 */
public class Painel extends javax.swing.JFrame {
    private JToolBar jToolBar1;

    /** Cria o novo formulario Painel */
    public Painel() {
        initComponents();

    }

    /**
     * Este metodo eh chamado no construtor
     * da classe para inicializar o formulario.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        grid = new Grid();
        robo = new Robo();
        jToolBar1 = new javax.swing.JToolBar();
        escreveVel = new java.awt.Label();
        MedeVel = new javax.swing.JProgressBar();
        MedeDist = new java.awt.Label();
        TextoDist = new javax.swing.JLabel();
        TextVel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema Fuzzy");
        addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(2000, 2000));

        grid.setBackground(new java.awt.Color(254, 254, 254));
        grid.setBorder(null);
        grid.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });

        grid.setBackground(new java.awt.Color(254, 254, 254));
        grid.setBorder(null);

        robo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/wally2.jpg"))); // NOI18N
        robo.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(grid);
        grid.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(robo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(2000, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(robo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(2000, Short.MAX_VALUE))
        );
        jToolBar1.setRollover(true);
        escreveVel.setText("");
        jToolBar1.add(escreveVel);

        MedeDist.setText("");

        TextoDist.setText("Distancia");

        TextVel.setText("Velocidade");

        jScrollPane1.setViewportView(grid);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TextoDist)
                            .addComponent(TextVel))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(MedeDist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(MedeVel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

//
//                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
//                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//                            .addComponent(MedeDist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
//                            .addComponent(MedeVel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
//        );
//        layout.setVerticalGroup(
//            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
//                .addContainerGap(66, Short.MAX_VALUE)
//                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
//                .addContainerGap())
//        );

       layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TextoDist)
                    .addComponent(MedeDist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(TextVel)
                        .addComponent(MedeVel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        grid.setLocalizacaoDoRobo(robo.getLocation());
        grid.setDeslocamento(39);
        pack();
    }// </editor-fold>

    private void formKeyPressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
   //     if(){
            //O PASSO DO ROBO VAI SER DEFINIDO PELA VELOCIDADE CALCULADA NO FUZZY
            double[] distancia = {0,0};

            distancia = robo.analisarTerreno(grid.getObstaculos(), evt.getKeyCode());
           // if(distancia[0]!=0){
               
          //  }
            double velocidade = new ControleFuzzy().calculaSaidaVelocidade(distancia[0], distancia[1]);
            escreveValoresTela(velocidade, distancia[1]);
            robo.movimentar(evt.getKeyCode(),velocidade);

 //       }
        grid.setLocalizacaoDoRobo(robo.getLocation());
        grid.setDeslocamento(evt.getKeyCode());
    }

    private void escreveValoresTela(double vel, double dist){
//        if(dist<0) dist=0;
        MedeVel.setMaximum(50);
        MedeDist.setText(String.valueOf(dist));
        escreveVel.setText(String.valueOf(vel));
        MedeVel.setValue((int)vel);
    }
    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        grid.desenhaObstaculo(evt.getPoint());
    }


    // Declaracao das variaveis;
    private Robo robo;
    private Grid grid;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label MedeDist;
    private javax.swing.JProgressBar MedeVel;
    private javax.swing.JLabel TextVel;
    private javax.swing.JLabel TextoDist;
    private java.awt.Label escreveVel;
    // Fim da declaracao de variaveis;
}
